//
//  TestEssayAnserView.m
//  ProjectDemo
//
//  Created by IOS3 on 2019/1/4.
//  Copyright © 2019 IOS3. All rights reserved.
//

#import "TestEssayAnserView.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "TestEssayAnserCell.h"



@interface TestEssayAnserView () <UICollectionViewDelegateLeftAlignedLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *datas;

@end

@implementation TestEssayAnserView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI {
 
    
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    self.collectionView.backgroundColor = UIColor.whiteColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:TestEssayAnserCell.class forCellWithReuseIdentifier:NSStringFromClass(TestEssayAnserCell.class)];
    self.collectionView.allowsMultipleSelection = YES;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TestEssayAnserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(TestEssayAnserCell.class) forIndexPath:indexPath];
    NSString *text = self.datas[indexPath.row];
    cell.text = text;
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *text = self.datas[indexPath.item];
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]}];
    CGFloat width = size.width + 20;
    
    return CGSizeMake(width, 40);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中: %ld", indexPath.item);
    if (self.tapCell) {
        self.tapCell(YES, indexPath);
    }
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"取消: %ld", indexPath.item);
    if (self.tapCell) {
        self.tapCell(NO, indexPath);
    }
}


- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
                   @"以", @"尔尔", @"三三三", @"死死死死", @"呜呜呜呜呜", @"六六六六六六", @"七七七七七七七七七七", @"叭叭叭叭叭叭叭叭叭叭叭叭吧", @"九九九", @"是是是", @"十一十一十一", @"十二", @"十三十三十三十三十三", @"十四", @"十五十五十五十五"
                   ];
    }
    return _datas;
}

@end



